import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fixnum/fixnum.dart' as $fixnum;

import 'package:provider/provider.dart';
import '../../../../lib_baseview/theme/jr.dart';
import '../../../../lib_baseview/widget/textfield/jr_edit_text.dart';
import '../../../../resource/language/generated/l10n.dart';
import '../../../m_chatroom.dart';

class ShareFriendItemWidget extends StatefulWidget {
  // final User? userItem;
  final BuildContext? context;
  // final MsgListUserItem? msgUserItem;
  final bool fromMsg; //聊天使用msgUserItem

  const ShareFriendItemWidget(
      {this.context,
      // this.userItem,
      // this.msgUserItem,
      this.fromMsg = false,
      Key? key})
      : super(key: key);

  @override
  State<ShareFriendItemWidget> createState() => _ShareFriendItemWidgetState();
}

class _ShareFriendItemWidgetState extends State<ShareFriendItemWidget> {
  bool hasInvite = false;

  @override
  Widget build(BuildContext context) {
    return _contactResultItemBuilder();
  }

  Widget _contactResultItemBuilder() {
    // String avatar = widget.userItem?.avatar ?? "";
    // String userNO = widget.userItem?.id.toString() ?? "";
    // int userId = widget.userItem?.id.toInt() ?? 0;
    // String showName = ((widget.userItem?.nickname != null)
    //         ? widget.userItem?.nickname
    //         : userNO) ??
    //     "";
    // bool hasRoom = widget.userItem?.hasCurrentChannel() ?? false;
    // int channelId = widget.userItem?.currentChannel?.channelId.toInt() ?? 0;
    // int age = widget.userItem?.age ?? 18;
    // String gender = widget.userItem?.gender ?? '';
    // String roomTag = widget.userItem?.currentChannel?.tag ?? '';
    // List<String> roomColors = widget.userItem?.currentChannel?.color ?? [];

    String avatar = "userItem";
    String userNO = "userItem";
    int userId = 0;
    String showName = "showName";
    bool hasRoom = false;
    int channelId = 0;
    int age = 18;
    String gender = '';
    String roomTag = '';
    List<String> roomColors = [];

    if (widget.fromMsg) {
      // avatar = widget.msgUserItem?.avatar ?? '';
      // userNO = widget.msgUserItem?.id.toString() ?? '';
      // userId = widget.msgUserItem?.id.toInt() ?? 0;
      // showName = widget.msgUserItem?.nickname ?? userNO;
      // channelId = widget.msgUserItem?.currentChannel?.channelId.toInt() ?? 0;
      // age = widget.msgUserItem?.age ?? 18;
      // gender = widget.msgUserItem?.gender ?? '';
      // roomTag = widget.msgUserItem?.currentChannel?.tag ?? '';
      // roomColors = widget.msgUserItem?.currentChannel?.color ?? [];
    }

    return Container(
      color: const Color(0xFFFFFFFF),
      padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 24),
      child: Row(
        children: [
          GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () {
                if (hasRoom) {
                  // 跳房间
                  // jumpToHallPage(
                  //   context!,
                  //   $fixnum.Int64(channelId),
                  //   false,
                  //   false,
                  // );
                } else {
                  // PersonalHomePage.show(context, userId,
                  //     visitRecord: true, refer: "room");
                }
              },
              child: Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  getUserImageWidget(
                      hasRoom, channelId, avatar, roomTag, roomColors),
                  const SizedBox(
                    width: 10,
                  ),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Container(
                            constraints: const BoxConstraints(maxWidth: 140),
                            child: Text(
                              showName,
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: const TextStyle(
                                  color: Color(0xFF333333),
                                  fontSize: 13,
                                  fontWeight: FontWeight.w500),
                            ),
                          ),
                          const SizedBox(
                            width: 4,
                          ),
                          // SexAgeLabel(
                          //     isMale: (gender == UserConstants.genderMan),
                          //     age: age.toString())
                        ],
                      ),
                      const SizedBox(
                        height: 10,
                      ),
                      Text(
                        "ID: $userNO",
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: const TextStyle(
                            fontSize: 11,
                            color: Color(0xFF666666),
                            fontWeight: FontWeight.w500),
                      )
                    ],
                  ),
                ],
              )),
          const Spacer(),
          const SizedBox(
            width: 8,
          ),
          if (!hasInvite)
            RTextButton(
              title: T.current.invite,
              onPressed: () {
                _inviteIntoRoom(context);
              },
              height: 28,
            ),
        ],
      ),
    );
  }

  ///
  /// 跳转聊天页面

  //获取用户头像
  Widget getUserImageWidget(bool hasRoom, int roomId, String avatar,
      String roomTag, List<String> roomColors) {
    if (!hasRoom) {
      return userImage(avatar);
    }

    if (hasRoom && roomId > 0) {
      return Stack(
        children: [
          Container(
            padding: const EdgeInsets.all(2),
            width: 56,
            height: 56,
            decoration: BoxDecoration(
              borderRadius: const BorderRadius.all(Radius.circular(28)),
              gradient: LinearGradient(
                  //渐变色
                  colors: getRoomStateColor(roomColors),
                  begin: Alignment.centerRight,
                  end: Alignment.centerLeft),
            ),
            child: Container(
              padding: const EdgeInsets.all(2),
              decoration: const BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(27)),
              ),
              child: userImage(avatar),
            ),
          ),
          Positioned(
            child: SizedBox(
              width: 56,
              height: 62,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  const Spacer(),
                  Container(
                    height: 18,
                    decoration: BoxDecoration(
                      borderRadius: const BorderRadius.all(Radius.circular(9)),
                      gradient: LinearGradient(
                        //渐变色
                        colors: getRoomStateColor(roomColors),
                        begin: Alignment.centerRight,
                        end: Alignment.centerLeft,
                      ),
                    ),
                    child: Container(
                      padding: const EdgeInsets.symmetric(horizontal: 4),
                      child: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Text('AnimationAudioLinesWidget'),
                          // AnimationAudioLinesWidget(),
                          const SizedBox(width: 2),
                          Expanded(
                            child: Text(
                              roomTag,
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              style: const TextStyle(
                                fontSize: 8,
                                color: Colors.white,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      );
    } else {
      return userImage(avatar);
    }
  }

  Widget userImage(String avatar) {
    return JR.img(
      avatar,
      width: 50,
      height: 50,
    );
    // return AvatarImage(url: avatar, width: 50, height: 50, borderRadius: 25);
  }

  //获取房间状态的颜色
  List<Color> getRoomStateColor(List<String> item) {
    List<Color> colorList = [];
    if (item.isNotEmpty) {
      for (var value in item) {
        colorList.add(Color(int.parse("0x$value")));
      }
    }
    if (colorList.isEmpty) {
      return const [Color(0xFF9A4BE6), Color(0xFF666FF4), Color(0xFF537BFE)];
    }
    return colorList;
  }

  _inviteIntoRoom(BuildContext context) {
    // Channel channel = context.read<RootProxyModel>().channel!;
    // Map<String, String>? params = {};
    // params["tit"] = channel.title;
    // params["imageUrl"] = channel.icon;
    // params["content"] = channel.theme;
    // params["tag"] = channel.roomLabelDesc;
    // params["number"] = "${channel.currentUserCount}";
    // params["channelId"] = "${channel.channelId}";
    // User user = UserManager.instance.currentUser!;
    // String emId = '';
    // String toName = '';
    // String toAvatar = '';
    // String toUserid = '';
    // if (widget.fromMsg) {
    //   emId = widget.msgUserItem?.emUsername ?? '';
    //   toName = widget.msgUserItem?.nickname ?? '';
    //   toAvatar = widget.msgUserItem?.avatar ?? '';
    //   toUserid = widget.msgUserItem?.id.toString() ?? '';
    // } else {
    //   emId = widget.userItem?.emUsername ?? '';
    //   toName = widget.userItem?.nickname ?? '';
    //   toAvatar = widget.userItem?.avatar ?? '';
    //   toUserid = widget.userItem?.id.toString() ?? '';
    // }

    // Map attributes = {
    //   "name": user.nickname,
    //   "avatar": user.avatar,
    //   "userid": user.id.toInt(),
    //   "emID": emId,
    //   "toName": toName,
    //   "toAvatar": toAvatar,
    //   "toUserid": toUserid,
    // };

    // var message = EMMessage.createCustomSendMessage(
    //     targetId: emId,
    //     event: CusTomMessage.ROOM_INVITE_MESSAGE,
    //     params: params);
    // message.attributes = attributes;
    // EMClient.getInstance.chatManager.sendMessage(message).then((value) {
    //   showToast(T.current!.invite_suc);
    //   hasInvite = true;
    //   setState(() {});
    // });
  }
}
